package com.zpyr.common;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogManager {

	private Logger logger;
	public LogManager() {
		logger = LoggerFactory.getLogger( getCaller() );
	}
	
	public void debug(String msg) {
		debug("{}", msg);
	}
	public void debug(String conf , String msg) {
		logger.debug(conf , msg);
	}
	
	public void error(String msg) {
		error("{}" , msg);
	}
	public void error(String conf , String msg) {
		logger.error(conf , msg);
	}
	
	public void info(String msg) {
		info("{}" , msg);
	}
	public void info(String conf , String msg) {
		logger.info(conf , msg);
	}

	public void warn(String msg) {
		warn("{}" , msg);
	}
	public void warn(String conf , String msg) {
		logger.warn(conf , msg);
	}
	
	private String getCaller() {
	      String caller = "----";
			
	      try {
	         throw new Exception();
	      } catch(Exception e) {
	         StringWriter sw = new StringWriter();
	         PrintWriter ew = new PrintWriter(sw);
	         e.printStackTrace(ew);
	         String thisClassName = getClass().getName();
	         try {
	            BufferedReader br = new BufferedReader(new StringReader(sw.toString()));
	            String str = null;
	            while((str = br.readLine()) != null) {
	               if(str.equals("java.lang.Exception")) continue;
	               if(str.indexOf(thisClassName) != -1) continue;
	               try {
	                  int i = str.indexOf("at ") + 3;
	                  caller = str.substring(i);
	                  i = caller.indexOf("(");
	                  caller = caller.substring(0, i);
	                  i = caller.lastIndexOf(".");
	                  caller = caller.substring(0, i);
	                  break;
	               } catch(Exception e2) {}
	            }
	         } catch(Exception e2) {}
	      }
	      return caller;
	   }
	
}
